package com.atguigu.mvc.controller.Handler;

import com.atguigu.mvc.controller.Service.EmpService;
import com.atguigu.mvc.entity.Department;
import com.atguigu.mvc.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author : zzf
 * @date : 2021/1/7
 */

@Controller
@RequestMapping("emp")
public class EmpHandler {
    @Autowired
    private EmpService empService;

    @RequestMapping("list")
    public String selectEmp(Model model){
        List<Employee> list =  empService.getEmpList();
        model.addAttribute("list",list);
        return "target";
    }

    @RequestMapping("del")
    public String delEmp(String id){
        empService.delEmp(id);
        return "redirect:list";
    }

    @RequestMapping("add/page")
    public String toAddPage(Model model){
        List<Department> deptList = empService.getDeptList();
        model.addAttribute("deptList",deptList);
        return "add";
    }

    @RequestMapping("add")
    public String addEmp(Employee employee){
        empService.addEmp(employee);
        return "redirect:list";
    }

    @RequestMapping("update")
    public String updateEmp(String id,Model model){
        Employee employee = empService.getEmp(id);
        List<Department> deptList = empService.getDeptList();
        model.addAttribute("employee",employee);
        model.addAttribute("deptList",deptList);
        return "edit";
    }

    @RequestMapping("updateEmp")
    public String updateEmp(Employee employee){
        empService.updateEmp(employee);
        return "redirect:list";
    }



}




